﻿using System.Collections;
using UnityEngine;
using System;
using System.Threading;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using IMIForUnity;


namespace IMIForUnity
{	
	public  partial class BodyMeaMgrImp
	{

		public float hipsWidth = 0;  
		public float hipsThickness=0f;

		 float hipsRatio=1.05f;
//		public ClusterSmoothTool hipsSmthWideness;
//		public ClusterSmoothTool hipsSmthThickness;

		public ArrayList hipsSmthWidthArr=new ArrayList();
		public ArrayList hipsSmthThicknessArr=new ArrayList();

		public float hipsWidetmp=0f;
		public float hitHoriExtRatio=0.5f;
		
		public void ProcessHipsFront (int userID, bool isDrawOnDepthMap)
		{


//hips wide process  before  12 01
//			//GetChestFrontWidth(userID);
//			Vector3 lefthip=GetJointSpacePos( userID,  
//			                                ImiSkeleton.Index.HIP_LEFT);
//			Vector3 righthip=GetJointSpacePos( userID,  
//			                                 ImiSkeleton.Index.HIP_RIGHT);
//
//			Vector3 hipHoriVec=righthip-lefthip;
//			Vector3 Adjustlefthip=lefthip-hipHoriVec*hitHoriExtRatio;
//			Vector3 Adjustrighthip=righthip+hipHoriVec*hitHoriExtRatio;
//			
//
//
//			hipsWidetmp=Mathf.Abs(Adjustlefthip.x - Adjustrighthip.x  ); 
////			hipsSmthWideness.InsertValue(hipsWidetmp);
////			hipsWidth=hipsSmthWideness.GetFilteredValue();



			//hipds width process after 12 01 . use a way similar to waist front
			int dw = DEPTHWIDTH;
			int dh = DEPTHHEIGHT;

			int lowhipsPosXleft=0;
			int lowhipsPosYleft=0;
			int lowhipsPosZleft=0;
			
			GetPlayerJointPixPos (userID, ref lowhipsPosXleft, ref lowhipsPosYleft, ref lowhipsPosZleft, 
			                     ImiSkeleton.Index.HIP_LEFT, isDrawOnDepthMap);


			int lowhipsPosXright=0;
			int lowhipsPosYright=0;
			int lowhipsPosZright=0;
			
			GetPlayerJointPixPos (userID, ref lowhipsPosXright, ref lowhipsPosYright, ref lowhipsPosZright, 
			                     ImiSkeleton.Index.HIP_RIGHT, isDrawOnDepthMap);


			int lowhipsPosXmid=(lowhipsPosXleft+lowhipsPosXright)/2;
			int lowhipsPosYmid=(lowhipsPosYleft+lowhipsPosYright)/2;
			int lowhipsPosZmid=(lowhipsPosZleft+lowhipsPosZright)/2; 

			
			
			if(IMIBodyMeaTool.DoesPixBelongToPlayers(dw,dh,lowhipsPosXmid,lowhipsPosYmid, depthPlayerDataOriginal))
			{
				Vector2 MinHIpsPixPos = IMIBodyMeaTool.GetContourMinEnd (dw, dh, lowhipsPosXmid, lowhipsPosYmid, depthPlayerDataOriginal, boundThreshval);
				Vector2 MaxHIpsPixPos = IMIBodyMeaTool.GetContourMaxEnd (dw, dh, lowhipsPosXmid, lowhipsPosYmid, depthPlayerDataOriginal, boundThreshval);
				
						
				Vector2 resultHIpsEndMin = Vector2.zero;  //this is the pix pos of 2 ends of HIps
				Vector2 resultHIpsEndMax = Vector2.zero;  //this is the pix pos of 2 ends of HIps
				
				IMIBodyMeaTool.GetFinalFrontEnds(dw,dh,depthPlayerDataOriginal, new Vector2(lowhipsPosXmid,lowhipsPosYmid), 
				                                 MinHIpsPixPos,MaxHIpsPixPos,ref resultHIpsEndMin,ref resultHIpsEndMax,10,4);   // 10 7
				
				
				
				if (isDrawOnDepthMap) {
					DrawHorilineOnUserDepthColor (allUsersDepthData, dw, dh, (int)MinHIpsPixPos.x, (int)MaxHIpsPixPos.x, (int)MaxHIpsPixPos.y, 255, 255, 0, 255);
					DrawHorilineOnUserDepthColor (allUsersDepthData, dw, dh, (int)resultHIpsEndMin.x, (int)resultHIpsEndMax.x, (int)resultHIpsEndMax.y, 255, 0, 0, 255);
				}
				

				int HIpsDepth=(depthPlayerDataOriginal [((int)(lowhipsPosYmid)) * dw + lowhipsPosXmid]>>3);  //in mm
				
				hipsWidetmp = GetThicknessBy2PixPoses (resultHIpsEndMin, resultHIpsEndMax, HIpsDepth);

			}



			hipsSmthWidthArr.Add(hipsWidetmp);
			
			if(isDrawOnDepthMap)
			{
				DrawJointsMarksOnDepthMap(userID,ImiSkeleton.Index.HIP_LEFT
				                          ,5,255,255,0,255);
				
				DrawJointsMarksOnDepthMap(userID,ImiSkeleton.Index.HIP_RIGHT
				                          ,5,255,0,155,255);
									
			}		
		}	




		void ProcessHipsSide(int userID, bool isDrawOnDepthMap)
		{
			int dw = DEPTHWIDTH;
			int dh = DEPTHHEIGHT;


			int hipsMidPixPosX=0;
			int hipsMidPixPosY=0;
			int hipsMidPixPosZ=0;
			
			GetPlayerJointPixPos (userID, ref hipsMidPixPosX, ref hipsMidPixPosY, ref hipsMidPixPosZ, 
			                     ImiSkeleton.Index.HIP_CENTER,false);

			int hipleftX=0;
			int hipleftY=0;
			int hipleftZ=0;
			
			GetPlayerJointPixPos (userID, ref hipleftX, ref hipleftY, ref hipleftZ, 
			                     ImiSkeleton.Index.HIP_LEFT,false);


			//To measure the hips thickness.
			int hipssideupx=RangeValue(hipsMidPixPosX, 0 ,dw);
			int hipssideupy=RangeValue(hipleftY, 0 ,dh);

			int hipssidedownx=RangeValue(hipsMidPixPosX, 0 ,dw);;
			int hipssidedowny=RangeValue((hipleftY+(hipleftY-hipsMidPixPosY)),0 ,dh); 



			Vector2 HipsUpPixPos = new Vector2(hipssideupx,hipssideupy );
			Vector2 HipsDownPixPos = new Vector2(hipssidedownx,hipssidedowny );

						
			//we add a adjusting deeping value on the waist surface depth.
				
			//method 1 get the thicknest part of the hips side	by using 2 pixposes
			int tempPlayerIndex0 = depthPlayerDataOriginal  [hipssidedowny *dw +hipssidedownx] & 7;
			int tempPlayerIndex1 = depthPlayerDataOriginal  [hipssideupy *dw +hipssideupx] & 7;
				
				
				//Only when the key pix pos symplify an player pixel.
			if((tempPlayerIndex0>0)&&(tempPlayerIndex1>0)&&(tempPlayerIndex0== tempPlayerIndex1 ))
			{
					
					float hipsSideTmp=GetThickestPartBy2PixPoses(dw,dh,depthPlayerDataOriginal,
					                                             boundThreshval,HipsUpPixPos,HipsDownPixPos,isDrawOnDepthMap);	
								
					
				//Debug.Log("+++++++++++++++++++++++++++hips side tmep: "+hipsSideTmp.ToString());
//					hipsSmthThickness.InsertValue (hipsSideTmp);
//					hipsThickness=hipsSmthThickness.GetFilteredValue();

				hipsSmthThicknessArr.Add(hipsSideTmp);
			 }


			if(isDrawOnDepthMap)
			{ 
				DrawPointOnDepthMap(640,480,(int)HipsUpPixPos.x,(int)HipsUpPixPos.y,
				                    7,allUsersDepthData,0,255,0,255);
						

				DrawPointOnDepthMap(640,480,(int)HipsDownPixPos.x,(int)HipsDownPixPos.y,
				                    7,allUsersDepthData,0,255,0,255);


				DrawPointOnDepthMap(640,480,hipsMidPixPosX,hipsMidPixPosY,
				                    7,allUsersDepthData,0,0,0,255);
				
				
				DrawPointOnDepthMap(640,480,hipleftX,hipleftY,
				                    7,allUsersDepthData,0,0,0,255);
			}		



			//method 2  get the thicknest part of the hips side	by using 1 pixposes
//			if((hipsMidPixPosX>0)&&(hipsMidPixPosX<dw)&&
//			   (hipleftY>0)&&(hipleftY<dh))
//			{
//				//we add a adjusting deeping value on the waist surface depth.
//				
//				
//				int tempPlayerIndex = depthPlayerDataOriginal  [hipleftY *dw +hipsMidPixPosX] & 7;
//				
//				
//				//Only when the key pix pos symplify an player pixel.
//				if(tempPlayerIndex>0)
//				{
//
//					int	hipsSideDepth =((depthPlayerDataOriginal [hipleftY * dw+ hipsMidPixPosX]) >> 3)
//							                       +adjustDeepingLengthForSideHips ;
//						
//
//
//
//					   float hipsSideTmp= GetThicknessFromOnePixPos(depthPlayerDataOriginal, 
//						                                                      dw, 
//						                                                      dh, 
//					                                                          hipsMidPixPosX,
//									                                             hipleftY,
//						                                                      boundThreshval,			               
//						                                                       hipsSideDepth, 
//						                                                      isDrawOnDepthMap);
//
//						hipsSmthThickness.InsertValue (hipsSideTmp);
//						hipsThickness=hipsSmthThickness.GetFilteredValue();
//				}
//			}
		}



		public float  GetHipsGirth( )
		{
			return IMIBodyMeaTool.CalculateHipsGirth(hipsWidth, hipsThickness,hipsRatio);
			
		}



		int RangeValue(int value, int min, int max)
		{
			if(value<min)value=min;
			if(value>=max)value=max-1;

			return value;
		}











	}
}
